package com.zzg.dao.impl;

import java.util.List;

import com.zzg.bean.User;
import com.zzg.dao.UserDAO;
import com.zzg.util.JDBCUtil;
import org.springframework.stereotype.Repository;

@Repository
public class UserDAOImpl implements UserDAO{
	/* 
	 * 找到所有的用户
	 */
	@Override
	public List<User> findAllUser() {
		return JDBCUtil.queryList(User.class,
				"select * from tb_user");
	}

	/*
	 * 增加用户 
	 */
	@Override
	public int addUser(User user) {
		return JDBCUtil.update("insert into tb_user(username,realname,address,tel) values (?,?,?,?)"
				,user.getUsername(),user.getRealname(),user.getAddress(),user.getTel());
	}

	/*
	 * 修改用户 
	 */
	@Override
	public int updateUser(User user) {
		return JDBCUtil.update("update tb_user set username = ?,realname = ?,address = ?,tel = ? where user_id=?"
				,user.getUsername(),user.getRealname(),user.getAddress(),user.getTel(),user.getUser_id());
	}

	/*
	 * 删除用户 
	 */
	@Override
	public int deleteUser(int id) {
		return JDBCUtil.update("delete from tb_user where user_id = ?",id);
	}

	@Override
	public User findUserById(int id) {
		return JDBCUtil.queryOne(User.class,
				"select * from tb_user where user_id =?",id);
	}

	@Override
	public User selectUserbyUsername(String username) {
		return JDBCUtil.queryOne(User.class,
				"select * from tb_user where username = ?",username);
	}

	@Override
	public int selectUserCount() {
		return JDBCUtil.queryCount("select count(*) from tb_user");
	}

	@Override
	public List<User> selectUsersLimit(int start, int length) {
		return JDBCUtil.queryList(User.class,
				"select * from tb_user limit ?,?",start,length);
	}

	@Override
	public List<User> findUserLikeName(String username) {
		return JDBCUtil.queryList(User.class,
				"select * from tb_user where username like ?","%"+username+"%");
	}

}
